package com.danielmiller.mygradebook;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MyGradeBook extends Activity{

	ArrayList<String> arrayList = new ArrayList<String>();
	SQLiteDatabase db;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Initialize_Database();
		
		//set Button Array Listener
		final Button button1 = (Button)findViewById(R.id.class1_button);
		final Button button2 = (Button)findViewById(R.id.class2_button);
		final Button button3 = (Button)findViewById(R.id.class3_button);
		final Button button4 = (Button)findViewById(R.id.class4_button);
		final Button button5 = (Button)findViewById(R.id.class5_button);
		final Button button6 = (Button)findViewById(R.id.class6_button);
		final TextView grade1 = (TextView)findViewById(R.id.grade1_textView);
		final TextView grade2 = (TextView)findViewById(R.id.grade2_textView);
		final TextView grade3 = (TextView)findViewById(R.id.grade3_textView);
		final TextView grade4 = (TextView)findViewById(R.id.grade4_textView);
		final TextView grade5 = (TextView)findViewById(R.id.grade5_textView);
		final TextView grade6 = (TextView)findViewById(R.id.grade6_textView);


		//set Button Text
		Cursor cur = db.rawQuery("SELECT class_name, class_overall FROM tbl_classes", null);
		int i = 0;
		String c1, c2;
		if (cur != null ) {
			if (cur.moveToFirst()){
				while (cur.isAfterLast() == false) {
					switch (i){
					case 1:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button1.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade1.setText(c2);
					case 2:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button2.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade2.setText(c2);
					case 3:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button3.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade3.setText(c2);
					case 4:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button4.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade4.setText(c2);
					case 5:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button5.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade5.setText(c2);
					case 6:
						c1 = cur.getString(cur.getColumnIndex("class_name"));
						button6.setText(c1);

						c2 = cur.getString(cur.getColumnIndex("class_overall"));
						grade6.setText(c2);
					}
					cur.moveToNext();
					i++;
				}
			}
		}
		cur.close();
		db.close();
		
		button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 1);
				startActivity(intent);
			}
		});
 
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 2);
				startActivity(intent);
			}
		});

		button3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 3);
				startActivity(intent);
			}
		});

		button4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 4);
				startActivity(intent);			
			}
		});

		button5.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 5);
				startActivity(intent);
			}
		});

		button6.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent;
				if (button1.getText().toString().contains("Add New Class")){
					intent = new Intent(getBaseContext(), AddClass.class);
				}else{
					intent = new Intent(getBaseContext(), EditClassOverview.class);
				}	
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", 6);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.export:
			Log.d("TAG", "Export!!");
			Intent intent2 = new Intent(this, ExportGradeBook.class);
			intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent2);
			return true;
		case R.id.scan:
			Log.d("TAG", "Scan!!");
			Intent intent3 = new Intent(this, ScanClass.class);
			intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent3);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void Initialize_Database() {
		// TODO Auto-generated method stub
		db = openOrCreateDatabase(
				"SEMESTER_DB.db"
				, SQLiteDatabase.CREATE_IF_NECESSARY
				, null
		);
		db.setVersion(1);
		db.setLocale(Locale.getDefault());
		db.setLockingEnabled(true);
		
		final String CLASS_TABLE = 
			"CREATE TABLE IF NOT EXISTS tbl_classes ("
			+ "class_id INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "class_name TEXT,"
			+ "class_overall DOUBLE" 
			+ "class_link TEXT;";
		db.execSQL(CLASS_TABLE);
		
		final String CATEGORY_TABLE = 
			"CREATE TABLE IF NOT EXISTS tbl_category ("
			+ "category_id INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "FOREIGN KEY (category_class_id) REFERENCES tbl_classes(class_id),"
			+ "category_name TEXT,"
			+ "category_weight INT(3));";
		db.execSQL(CATEGORY_TABLE);
		
		final String GRADE_TABLE = 
			"CREATE TABLE IF NOT EXISTS tbl_grades ("
			+ "grade_id INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "FOREIGN KEY (grade_class_id) REFERENCES tbl_classes(class_id),"
			+ "FOREIGN KEY (grade_category_id) REFERENCES tbl_category(category_id),"
			+ "grade_name TEXT,"
			+ "grade_recv INT(3),"
			+ "grade_poss INT(3),"
			+ "grade_pic TEXT);";
		db.execSQL(GRADE_TABLE);
	}
}